package com.itstyle.quartz.web;

import com.itstyle.quartz.entity.Result;
import com.itstyle.quartz.kettleUtil.FileReadUtil;
import com.itstyle.quartz.service.dao.ACLineDotService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * @author ycj
 * @version 1.0
 * @description
 * @date 2020/10/20 18:02
 */
@RestController
@RequestMapping("/file")
public class FileController{


    private final Log log = LogFactory.getLog(FileController.class);

    @Autowired
    private ACLineDotService acLineDotService;

    @PostMapping("/dfb")
    public Result test() {

        return Result.ok();
    }

    @PostMapping("/abc")
    public Result read() {

        String realpath = "D:\\dianli\\工作材料\\k";

        Result result = null;

        try {
            //获取目标文件夹下文件
            File[] file1 = FileReadUtil.getFile(realpath);

            for (int i = 0; i < file1.length; i++) {
                String path = file1[i].getPath();

                String[] split = path.split("\\.");
                if (split[split.length-1].equals("eh") || split[split.length-1].equals("status") ) {
                    log.info("此文件为eh或status文件，不保存其数据:"+path);
                    continue;
                }

                File file = new File(path);
                if (!file.exists() || file.length() == 0 ) {
                    log.info("文件不存在或者为空:"+path);
                    continue;
                }

                result = save(path);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }

    public Result save(String path) {

        String[] split = path.split("\\.");
        String type = split[1];

        HashMap<String, List<String>> read = null;
        try {
            read = FileReadUtil.read(path);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("文件读取失败:" + path);
            return Result.error("文件读取失败:" + path);
        }
        List<String> values = read.get("value");
        List<String> tables = read.get("table");

        try {
            if (!type.equals("txt")) {
//                acLineDotService.save(tables, values, type);
            } else {
                acLineDotService.txtsave(tables, values, type);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("文件数据保存失败:" + path);
            return Result.error("文件数据保存失败:" + path);
        }
        return Result.ok();
    }

    @PostMapping("/a")
    public void savea() {
        acLineDotService.saveFileName("asd");
    }
}
